package com.atomos.strategy.initialize.display
{
	import com.atomos.log.AtomosDebug;
	import com.atomos.particle.Particle;
	import com.atomos.strategy.initialize.InitializeStrategy;
	import com.bourre.error.ClassCastException;
	import com.bourre.error.NullPointerException;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import com.atomos.particle.DisplayObjectParticle;

	public class DisplayObjectInitalizer implements InitializeStrategy
	{
		static public const BACK : Number = 0;
		static public const FRONT : Number = 1;
		
		protected var _cType : Class;
		protected var _doParent : DisplayObjectContainer;
		protected var _nMethod : Number;
		
		public function DisplayObjectInitalizer ( doType : Class, doParent : DisplayObjectContainer, method : Number = FRONT )
		{
			_checkDisplayObjectType ( doType );
			_cType = doType;
			_doParent = doParent;
			_nMethod = method;
		} 
		
		public function initialize( particle : Particle ) : void
		{
			var doObj : DisplayObject = new _cType() as DisplayObject;
			
			( particle as DisplayObjectParticle ).displayObject = doObj;
			
			switch ( _nMethod )
			{
				case BACK : _doParent.addChildAt( doObj, 0 ); break;
				case FRONT : _doParent.addChild( doObj ); break;
			}
		}
		
		protected function _checkDisplayObjectType ( c : Class ) : void
		{
			if( c == null )
			{				
				AtomosDebug.ERROR( "DisplayObject's class passed to " + this + " is null" );
				
				throw new NullPointerException ( "DisplayObject's class passed to " + this + " is null" );
			}
			else if( ( new c () as DisplayObject ) == null )
			{
				AtomosDebug.ERROR( "DisplayObject's class passed to " + this + 
								   " is not a subclass of the flash.display::DisplayObject class : " + 
								   new c() );
				
				throw new ClassCastException ( "DisplayObject's class passed to " + this + 
											   " is not a subclass of the flash.display::DisplayObject class : " + 
											   new c() );
			}
		}
	}
}